package ar.edu.unlp.linti.batteryconsume;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;

public class BaterySevice extends Service {
	public final static String TAG = "Benchmark Batery Service";
	private WakeLock wakeLock;
	private PowerManager powerManager;
	private final Handler mHandler = new Handler();
	private Runnable mTimer1/* , mTimer2 */;
	private long iterations = 0l;
	private String result = "";

	private void doStep() {
		LinpackLoop l = new LinpackLoop();
		l.mInfo = new Bundle();
		result = l.run_benchmark();
		iterations++;
	}

	public boolean insertBatteryData(ContentValues values) {
		DatabaseHelper mOpenHelper = null;
		SQLiteDatabase db = null;
		boolean rc = true;
		try {
			mOpenHelper = new DatabaseHelper(this);
			db = mOpenHelper.getWritableDatabase();
			Long rowid = db.insert(DatabaseHelper.LEVELS_TABLE, null, values);
			if (rowid < 0) {
				rc = false;
			}
		} catch (Exception e) {
			rc = false;
		}
		if (db != null)
			db.close();
		if (mOpenHelper != null)
			mOpenHelper.close();
		return rc;
	}

	BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(android.content.Intent.ACTION_BATTERY_CHANGED)) {
				Log.e(TAG, "ACTION_BATTERY_CHANGED");
				Bundle extras = intent.getExtras();
				Log.e(TAG, "keys: " + extras.keySet());
				ContentValues values = new ContentValues();
				Long now = Long.valueOf(System.currentTimeMillis());
				values.put("sampletime", now);
				values.put("level", extras.getInt("level"));
				values.put("voltage", extras.getInt("voltage"));
				values.put("temperature", extras.getInt("temperature"));
				values.put("plugged", extras.getInt("plugged"));
				values.put("iterations", iterations);// iterations
				values.put("result", result);// result
				insertBatteryData(values);
				iterations = 0;
				//lastlevel = extras.getInt("level");
			}
		}

	};

	@Override
	public void onCreate() {
		super.onCreate();
		
		Log.e(TAG, "Started");
		
		IntentFilter batteryFilter = new IntentFilter();
		batteryFilter.addAction(android.content.Intent.ACTION_BATTERY_CHANGED);
		registerReceiver(mIntentReceiver, batteryFilter);

		/**
		 * Wake lock level: Ensures that the CPU is running; the screen and
		 * keyboard backlight will be allowed to go off. If the user presses the
		 * power button, then the screen will be turned off but the CPU will be
		 * kept on until all partial wake locks have been released.
		 **/
		powerManager = (PowerManager) getSystemService(POWER_SERVICE);
		wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
				"MyWakelockTag");
		wakeLock.acquire();
		/********************************************************/
		mTimer1 = new Runnable() {
			public void run() {
				doStep();
				mHandler.postDelayed(this, 50);
			}
		};
		mHandler.postDelayed(mTimer1, 50);

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		wakeLock.release();
	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
}
